home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_10 / finedif.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  801 b   |  30 lines  |  [MATF/MATL]

  1. function U = finedif(f,g,a,b,c,n,m)
  2. % U = finedif(f,g,a,b,c,n,m)
  3. % Finite difference solution to the wave equation.
  4. % f  is a function, input.
  5. % g  is a function, input.
  6. % a  is the width of [0 a], input.
  7. % b  is the width of [0 b], input.
  8. % c  is the constant in the wave equation, input.
  9. % n  is the number of grid points over [0 a], input.
  10. % m  is the number of grid points over [0 b], input.
  11. % U  is the solution matrix, output.
  12. h = a/(n-1);
  13. k = b/(m-1);
  14. r = c*k/h;
  15. r2 = r^2;
  16. r22 = r^2/2;
  17. s1 = 1 - r^2;
  18. s2 = 2 - 2*r^2;
  19. U = zeros(n,m);
  20. for i=2:(n-1),
  21.   U(i,1) = feval(f,h*(i-1));
  22.   U(i,2) = s1*feval(f,h*(i-1)) + k*feval(g,h*(i-1)) ...
  23.          + r22*(feval(f,h*(i)) + feval(f,h*(i-2)));
  24. end
  25. for j=3:m,
  26.   for i=2:(n-1),
  27.     U(i,j) = s2*U(i,j-1) + r2*(U(i-1,j-1) + U(i+1,j-1)) - U(i,j-2);
  28.   end
  29. end
  30.